package com.wontube.smr.remote.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.wontube.client.extension.utils.StringUtils;
import com.wontube.client.modle.vo.PagenateInfo;
import com.wontube.core.annotation.CacheSupport;
import com.wontube.core.annotation.NoLoginSupport;
import com.wontube.core.pub.servlet.handlers.BasicHandler;
import com.wontube.smr.bo.constants.SiteConstants;
import com.wontube.smr.bo.sites.GenreSelectBO;
import com.wontube.smr.client.model.po.Genre;
import com.wontube.smr.client.model.po.Movie;
import com.wontube.smr.client.model.vo.MovieItems;
import com.wontube.smr.dao.bo.MovieSelectDao;
import com.wontube.smr.web.client.IMovieWebHandler;
import com.wontube.smr.web.client.model.bo.QueryItemParams;

/**
 * @author tanggf(2012-7-26)
 * 
 */
@NoLoginSupport
public final class MovieWebHandler extends BasicHandler implements IMovieWebHandler {
	@Override
	@CacheSupport(timeToLiveSeconds = 12 * 7 * 3600)
	public List<Genre> listMovieGenres() {
		return GenreSelectBO.selectRemandedMovieGenres();
	}
	
	@Override
	@CacheSupport(timeToLiveSeconds = 12 * 7 * 3600)
	public List<Genre> listMovieTrailerGenres() {
		//TODO 暂时使用I18N的genres
		return GenreSelectBO.selectI18nTrailerGenres();
	}

	@Override
	@CacheSupport(timeToLiveSeconds = 6 * 3600)
	public Map<String, Object> listMovieWebHome() {
		Map<String, Object> results = new HashMap<String, Object>();
		MovieItems huluItems = MovieSelectDao.selectHuluPopularMovies(new PagenateInfo(1, 20));
		results.put("huluPopItems", huluItems);
		MovieItems mfItems = MovieSelectDao.selectWeekPopular(SiteConstants.WEBSITE_MOVIEFONE, Movie.TYPE_TRAILER,
				new PagenateInfo(1, 10));

		results.put("moviefoneTrailler", mfItems);
		return results;
	}

	@Override
	@CacheSupport(timeToLiveSeconds = 6 * 3600)
	public MovieItems listFullLengthMovies(QueryItemParams params, PagenateInfo pageInfo) {
		params.setGenre(GenreSelectBO.joinMovieGenres(params.getGenre()));
		MovieItems items = MovieSelectDao.selectHuluMovies(params, pageInfo);
		if (params.isNeedGenres()) {
			items.setGenres(listMovieGenres());
		}
		return items;
	}

	@Override
	@CacheSupport(timeToLiveSeconds = 6 * 3600)
	public MovieItems listThisYearTraillers(int year, int month, PagenateInfo pageInfo) {
		return MovieSelectDao.selectMoviefoneTrailler(year, month, pageInfo);
	}

	@Override
	@CacheSupport(timeToLiveSeconds = 6 * 3600)
	public MovieItems listMovieTraillers(QueryItemParams params, PagenateInfo pageInfo) {
		//TODO 暂时使用I18N的genres
		params.setGenre(GenreSelectBO.joinImdbI18nMovieGenres((params.getGenre())));		
		MovieItems items = MovieSelectDao.selectMovieTraillers(params, pageInfo);
		if (params.isNeedGenres()) {
			items.setGenres(listMovieTrailerGenres());
		}
		return items;
	}
	
	@Override
	@CacheSupport(timeToLiveSeconds = 6 * 3600)
	public Movie getMovie(String movieId) {
		if (StringUtils.isNullOrEmpty(movieId)) {
			return null;
		}
		return MovieSelectDao.getMovie(movieId);
	}	

	//====i18n=====
	@Override	
	@CacheSupport(timeToLiveSeconds = 6 * 3600)
	public MovieItems listI18nMovieTraillers(QueryItemParams params, PagenateInfo pageInfo) {
		params.setGenre(GenreSelectBO.joinImdbI18nMovieGenres(params.getGenre()));
		MovieItems items = MovieSelectDao.selectMovieImdbTraillers(params, pageInfo);
		if (params.isNeedGenres()) {
			items.setGenres(listI18nTrailerGenres());
		}
		return items;
	}
	
	@Override
	@CacheSupport(timeToLiveSeconds = 6 * 3600)
	public MovieItems listI18nMovies(QueryItemParams params, PagenateInfo pageInfo) {
		params.setGenre(GenreSelectBO.joinYoutubeI18nMovieGenres(params.getGenre()));
		MovieItems items = MovieSelectDao.selectYoutubeMovie(params, pageInfo);
		if (params.isNeedGenres()) {
			items.setGenres(listI18nMovieGenres());
		}
		return items;
	}

	@Override
	@CacheSupport(timeToLiveSeconds = 12 * 7 * 3600)
	public List<Genre> listI18nMovieGenres() {
		return GenreSelectBO.selectI18nMovieGenres();
	}

	@Override
	@CacheSupport(timeToLiveSeconds = 12 * 7 * 3600)
	public List<Genre> listI18nTrailerGenres() {
		return GenreSelectBO.selectI18nTrailerGenres();
	}
}
